package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.EmployeeNoticeMapper;
import cn.wolfcode.mapper.NoticeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private EmployeeNoticeMapper employeeNoticeMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void save(Notice notice) {
            notice.setCreat(UserContext.getContextUser());
            notice.setCreatTime(new Date());
            notice.setIssueStatus(false);
            noticeMapper.insert(notice);
    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice get(Long id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Notice> listAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public PageInfo<Notice> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        Employee employee = UserContext.getContextUser();
        List<Notice> notices = noticeMapper.selectForList(employee.getId(),qo,employee.isAdmin()); //里面不需要自己写limit
        return new PageInfo<Notice>(notices);
    }

    @Override
    public List<Notice> preHandler(List<Notice> list) {
        ArrayList<Notice> list1 = new ArrayList<>();
        for (Notice notice : list) {
            if(notice.getIssueStatus()){
                list1.add(notice);
            }
        }
        return list1;
    }

    @Override
    public void insertRelation(Long id) {
        List<Employee> employees = employeeService.listAll();
        for (Employee employee : employees) {
            employeeNoticeMapper.insert(id,employee.getId());
        }
    }

    @Override
    public void hasRead(Long id, Long id1) {
        employeeNoticeMapper.update(id,id1);
    }

    @Override
    public void updateStatus(Boolean status, Long id) {
        noticeMapper.updateStatus(status,id);
    }

    @Override
    public void restReadStatus(Long id) {
        employeeNoticeMapper.restReadStatus(id);
    }

    @Override
    public int getUnreadNum(Long id) {
        return employeeMapper.getUnreadNum(id);
    }

    @Override
    public List<Notice> getUnread(Long id) {
        return noticeMapper.getUnread(id);
    }
}
